#!/usr/bin/env bash
set -u

RED="\x1B[31m"
GREEN="\x1B[32m"
YELLOW="\x1B[33m"
RESET="\x1B[0m"

DEFAULT_URL_BASE="https://get.please.build"

OS="$(uname)"

if [[ $OS == "Darwin" ]]; then
    # switch between mac amd64/arm64 
    ARCH="$(uname -m)"
else
    # default to amd64 on other operating systems
    # because we only build intel binaries
    ARCH="amd64"
fi

# Check PLZ_CONFIG_PROFILE or fall back to arguments for a profile.
PROFILE="${PLZ_CONFIG_PROFILE:-$(sed -E 's/.*--profile[= ]([^ ]+).*/\1/g' <<< "$*")}"

# Config files on order of precedence high to low.
CONFIGS=(
  ".plzconfig.local"
  "${PROFILE:+.plzconfig.$PROFILE}"
  ".plzconfig_${OS}_${ARCH}"
  ".plzconfig"
  "$HOME/.config/please/plzconfig"
  "/etc/please/plzconfig"
)

function read_config() {
   grep -i "$1" "${CONFIGS[@]}" 2>/dev/null | head -n 1
}

# We might already have it downloaded...
LOCATION="$(read_config "^location" | cut -d '=' -f 2 | tr -d ' ')"
if [ -z "$LOCATION" ]; then
    if [ -z "$HOME" ]; then
        echo -e >&2 "${RED}\$HOME not set, not sure where to look for Please.${RESET}"
        exit 1
    fi
    LOCATION="${HOME}/.please"
else
    # It can contain a literal ~, need to explicitly handle that.
    LOCATION="${LOCATION/\~/$HOME}"
fi
# If this exists at any version, let it handle any update.
TARGET="${LOCATION}/please"
if [ -f "$TARGET" ]; then
    exec "$TARGET" ${PLZ_ARGS:-} "$@"
fi

URL_BASE="$(read_config "^downloadlocation" | cut -d '=' -f 2 | tr -d ' ')"
if [ -z "$URL_BASE" ]; then
    URL_BASE=$DEFAULT_URL_BASE
fi
URL_BASE="${URL_BASE%/}"

VERSION="$(read_config "^version[^a-z]")"
VERSION="${VERSION#*=}"    # Strip until after first =
VERSION="${VERSION/ /}"    # Remove all spaces
VERSION="${VERSION#>=}"    # Strip any initial >=
if [ -z "$VERSION" ]; then
    echo -e >&2 "${YELLOW}Can't determine version, will use latest.${RESET}"
    VERSION=$(curl -fsSL ${URL_BASE}/latest_version)
fi

# Find the os / arch to download. You can do this quite nicely with go env
# but we use this script on machines that don't necessarily have Go itself.
if [ "$OS" = "Linux" ]; then
    GOOS="linux"
elif [ "$OS" = "Darwin" ]; then
    GOOS="darwin"
elif [ "$OS" = "FreeBSD" ]; then
    GOOS="freebsd"
else
    echo -e >&2 "${RED}Unknown operating system $OS${RESET}"
    exit 1
fi

PLEASE_URL="${URL_BASE}/${GOOS}_${ARCH}/${VERSION}/please_${VERSION}.tar.xz"
DIR="${LOCATION}/${VERSION}"
# Potentially we could reuse this but it's easier not to really.
if [ ! -d "$DIR" ]; then
    rm -rf "$DIR"
fi
echo -e >&2 "${GREEN}Downloading Please ${VERSION} to ${DIR}...${RESET}"
mkdir -p "$DIR"
curl -fsSL "${PLEASE_URL}" | tar -xJpf- --strip-components=1 -C "$DIR"
# Link it all back up a dir
for x in $(ls "$DIR"); do
    ln -sf "${DIR}/${x}" "$LOCATION"
done
echo -e >&2 "${GREEN}Should be good to go now, running plz...${RESET}"
exec "$TARGET" ${PLZ_ARGS:-} "$@"
